const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    page: 1,
    orderList: [],
    type: "",
    height: 0,
    shopimgUrl: app.globalData.shopimgUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    });
    let orderStatus = options.orderStatus;
    if (orderStatus) {
      this.setData({
        type: orderStatus
      })
      console.log(this.data)
      this.data.orderList = [];
      this.data.page = 1;
      this.queryOrderList(orderStatus);
    } else {
      this.setData({
        type: "all"
      })
      this.queryAll();
    }

  },

  queryAll() {
    this.queryOrderList("one");
    this.queryOrderList("two");
    this.queryOrderList("three");
    this.queryOrderList("four");
  },


  changeTabs(event) {
    console.log(event)
    const type = event.detail.activeKey;
    this.setData({
      orderList: [],
      page: 1,
      type: type
    })
    if ("all" == type) {
      this.queryAll();
    } else {
      this.queryOrderList(type);
    }
  },


  /* 查询订单列表 */
  queryOrderList(type) {
    let storeId = wx.getStorageSync('storeId');
    let size = this.data.size;
    let page = this.data.page;
    if (!storeId) {
      wx.navigateTo({
        url: "/pages/login/index"
      });
    }
    let status = 1;
    let statusRemark = ""
    let btnName = ""
    let btnName1 = ""
    let btnName2 = ""
    if ("one" === type) {
      status = 1;
      statusRemark = "待接单";
      btnName = "接单";
      btnName1 = "拒绝";
    } else if ("two" === type) {
      status = 2;
      statusRemark = "待核销";
      btnName = "";//门店端没有去核销功能
      btnName1 = "取消订单";
    } else if ("three" === type) {
      status = 3;
      statusRemark = "已完成"
      btnName2 = "查看评价";
      btnName1 = "删除订单";
    } else if ("four" === type) {
      status = 4;
      statusRemark = "已退款"
      btnName2 = "删除订单";
    }
    _request.get('/order/getOrderPage', {
      storeId: storeId,
      page: page,
      size: size,
      status: status
    }).then((res) => {
      if (res.data.data != null) {
        let list = res.data.data.map(item => {
          let realName = btnName
          let orderDate = this.getYMDHMS(item.ordertime)
          let stratDate = this.getYMDHMS(item.starttime)
          let payamount = item.payamount || 0
          if (status == 3) {
            realName = "查看评价"
          }
          if (status == 1) {
            //  0 平台 1 店内
            if (item.nexus == 0) {
              realName = "提醒接单"
            } else {
              realName = "接单"
            }
          }else if (status == 4){
            realName = "查看详情"
          }
          return {
            ...item,
            statusRemark,
            orderDate,
            stratDate,
            btnName: realName,
            payamount: payamount.toFixed(2)
          }
        })
        this.setData({
          orderList: [...this.data.orderList, ...list]
        })
        console.log(this.data.orderList)
      }

    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  getYMDHMS(timestamp) {
    if (!timestamp) {
      return "";
    }
    let time = new Date(timestamp)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    let hours = time.getHours()
    let minute = time.getMinutes()
    let second = time.getSeconds()

    if (month < 10) {
      month = '0' + month
    }
    if (date < 10) {
      date = '0' + date
    }
    if (hours < 10) {
      hours = '0' + hours
    }
    if (minute < 10) {
      minute = '0' + minute
    }
    if (second < 10) {
      second = '0' + second
    }
    return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
  },

  /* 订单详情 */
  querOrderDetail(event) {
    console.log(event);
    var orderId = event.currentTarget.dataset.orderId;
    const statusRemark = event.currentTarget.dataset.statusRemark;
    const orderBtn = event.currentTarget.dataset.orderBtn;
    wx.navigateTo({
      url: '/pages/orderdetail/orderdetail?orderId=' + orderId + '&orderStatus=' + statusRemark + '&orderBtn=' + orderBtn
    })
  },

  onClickBtn(event) {
    var orderId = event.currentTarget.dataset.orderId;
    const orderBtn = event.currentTarget.dataset.orderBtn;
    if (orderBtn && "查看评价" == orderBtn) {
      wx.navigateTo({
        url: '/pages/commentdetail/commentdetail?orderId=' + event.currentTarget.dataset.orderId
      })
    } else if (orderBtn && ("接单" == orderBtn)){
      _request.get('/stylistOrder/acceptOrder', {
        orderId: orderId
      }).then((res) => {
        if (res.data.code === 200) {
          wx.showToast({
            title: '接单成功',
            icon: 'success',
            duration: 2000
          })
          this.setData({
            orderList:[],
            page: 1,
          })
          this.queryOrderList('one');
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'error',
            duration: 2000
          })
        }
      })    
    } else {
      this.querOrderDetail(event);
    }

  },

  more: function () {
    this.data.page++;
    this.queryOrderList(this.data.type);
  }
})